﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using WaterBill.BUS;
using WaterBill.DTO;
using WaterBill.GUI;
using DevExpress.XtraEditors;
using System.Threading;

namespace WaterBill
{
    public partial class frmMain : RibbonForm
    {
        BUS_Database busDB = new BUS_Database();
        public frmMain()
        {
            InitializeComponent();
            InitSkinGallery();
            InitGrid();
            Thread.Sleep(8000);
        }
        void InitSkinGallery()
        {
            SkinHelper.InitSkinGallery(rgbiSkins, true);
        }
        void InitGrid()
        {
          
        }

        public bool checkAddForm(string Text)
        {
            bool add = true;
            if (xtraTabbedMdiManager.Pages.Count > 0)
            {
                for (int i = 0; i < xtraTabbedMdiManager.Pages.Count; i++)
                {
                    if (xtraTabbedMdiManager.Pages[i].Text == Text)
                    {
                        add = false;
                        xtraTabbedMdiManager.SelectedPage = xtraTabbedMdiManager.Pages[i];
                        return add;
                    }
                }
            }
            return add;
        }

        public void addForm(string formName, Form child)
        {
            if (checkAddForm(formName))
            {
                child.Text = formName;
                child.TopLevel = false;
                child.MdiParent = this;
                child.Show();
            }
        }

        public static DataTable _DtUser = new DataTable();

        private void frmMain_Load(object sender, EventArgs e)
        {
            if (busDB.BUS_Database_checkExistFileSinfo() && !busDB.BUS_Database_checkNotExistDatabase())
            {
                iSettingConnect.Visibility = BarItemVisibility.Never;
                bsiUsername.Visibility = BarItemVisibility.Never;
                iLogin.Visibility = BarItemVisibility.Always;
                iLogout.Visibility = BarItemVisibility.Never;
                frmLogin frmLogin = new frmLogin();
                frmLogin.ShowDialog();
            }
            else
            {
                iSettingConnect.Visibility = BarItemVisibility.Always;
                bsiUsername.Visibility = BarItemVisibility.Never;
                iLogin.Visibility = BarItemVisibility.Never;
                iLogout.Visibility = BarItemVisibility.Never;
                frmConnectDatabase frmConn = new frmConnectDatabase();
                frmConn.ShowDialog();                
            } 
        }

        public void ShowMenuMain()
        {
            iChangePassword.Enabled = true;
            iManageUser.Enabled = true;
            iChangePassword.Enabled = true;
            iRestoreDatabase.Enabled = true;
            iBackupDatabase.Enabled = true;
            iManageHamlet.Enabled = true;
            iManageNest.Enabled = true;
            iCharge.Enabled = true;
            iManageCustomer.Enabled = true;
            iManageOrder.Enabled = true;
            iImportOrder.Enabled = true;
            iExportOrder.Enabled = true;
            iLiabilities.Enabled = true;
            iStats.Enabled = true;
            bsiUsername.Visibility = BarItemVisibility.Always;
            iLogin.Visibility = BarItemVisibility.Never;
            iLogout.Visibility = BarItemVisibility.Always;
            if (_DtUser.Rows.Count > 0)
            {
                bsiUsername.Caption = _DtUser.Rows[0]["FullName"].ToString();
                if (_DtUser.Rows[0]["NamePermission"].ToString() == "ADMIN")
                {
                    iManageUser.Enabled = true;
                    iRestoreDatabase.Enabled = true;
                    iBackupDatabase.Enabled = true;
                    iStats.Enabled = true;
                }
                else
                {
                    iManageUser.Enabled = false;
                    iRestoreDatabase.Enabled = false;
                    iBackupDatabase.Enabled = false;
                    iStats.Enabled = false;
                }
            }
        }

        public void HideMenuMain()
        {
            iManageUser.Enabled = false;
            iChangePassword.Enabled = false;
            iRestoreDatabase.Enabled = false;
            iBackupDatabase.Enabled = false;
            iManageHamlet.Enabled = false;
            iManageNest.Enabled = false;
            iCharge.Enabled = false;
            iManageCustomer.Enabled = false;
            iManageOrder.Enabled = false;
            iImportOrder.Enabled = false;
            iExportOrder.Enabled = false;
            iLiabilities.Enabled = false;
            iStats.Enabled = false;
            bsiUsername.Visibility = BarItemVisibility.Never;
            iLogin.Visibility = BarItemVisibility.Always;
            iLogout.Visibility = BarItemVisibility.Never;
        }

        public void SettingConnectSuccess()
        {
            iSettingConnect.Visibility = BarItemVisibility.Never;
            iLogin.Visibility = BarItemVisibility.Always;
        }
        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason != CloseReason.ApplicationExitCall)
            {
                if (XtraMessageBox.Show("Bạn có chắc là muốn thoát ứng dụng?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
                {
                    e.Cancel = true;
                }
                else
                {
                    Application.Exit();
                }
            }
                 
        }

        private void iExit_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (XtraMessageBox.Show("Bạn có chắc là muốn thoát ứng dụng?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                Application.Exit();
            }   
        }

        private void iLogout_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.HideMenuMain();
            frmLogin frm = new frmLogin();
            frm.ShowDialog();
        }

        private void iChangePassword_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmChangePassword frm = new frmChangePassword();
            frm.ShowDialog();
        }

        private void iManageUser_ItemClick(object sender, ItemClickEventArgs e)
        {
            splashScreenManager1.ShowWaitForm();
            frmManageUser frm = new frmManageUser();
            addForm("QUẢN LÝ NGƯỜI DÙNG", frm);
            splashScreenManager1.CloseWaitForm();
        }

        private void iManageCustomer_ItemClick(object sender, ItemClickEventArgs e)
        {
            splashScreenManager1.ShowWaitForm();
            frmManageCustomer frm = new frmManageCustomer();
            addForm("QUẢN LÝ KHÁCH HÀNG", frm);
            splashScreenManager1.CloseWaitForm();
        }

        private void iBackupDatabase_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmBackup frm = new frmBackup();
            frmBackup._Backup = true;
            frm.ShowDialog();
        }

        private void iRestoreDatabase_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmBackup frm = new frmBackup();
            frmBackup._Backup = false;
            frm.ShowDialog();
        }

        private void iManageOrder_ItemClick(object sender, ItemClickEventArgs e)
        {
            splashScreenManager1.ShowWaitForm();
            frmManageBill frm = new frmManageBill();
            addForm("QUẢN LÝ HÓA ĐƠN", frm);
            splashScreenManager1.CloseWaitForm();
        }

        private void iManageHamlet_ItemClick(object sender, ItemClickEventArgs e)
        {
            splashScreenManager1.ShowWaitForm();
            frmHamlet frm = new frmHamlet();
            addForm("QUẢN LÝ ẤP", frm);
            splashScreenManager1.CloseWaitForm();
        }

        private void iManageNest_ItemClick(object sender, ItemClickEventArgs e)
        {
            splashScreenManager1.ShowWaitForm();
            frmNest frm = new frmNest();
            addForm("QUẢN LÝ TỔ", frm);
            splashScreenManager1.CloseWaitForm();
        }

        private void iImportOrder_ItemClick(object sender, ItemClickEventArgs e)
        {
            splashScreenManager1.ShowWaitForm();
            frmImportBill frm = new frmImportBill();
            addForm("NHẬP HÓA ĐƠN", frm);
            splashScreenManager1.CloseWaitForm();
        }

        private void iExportOrder_ItemClick(object sender, ItemClickEventArgs e)
        {
            splashScreenManager1.ShowWaitForm();
            frmExportBill frm = new frmExportBill();
            addForm("XUẤT HÓA ĐƠN", frm);
            splashScreenManager1.CloseWaitForm();
        }

        private void iInfoSoftware_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmInfoSoftware frm = new frmInfoSoftware();
            frm.ShowDialog();
        }

        private void iCharge_ItemClick(object sender, ItemClickEventArgs e)
        {
            splashScreenManager1.ShowWaitForm();
            frmCharge frm = new frmCharge();
            addForm("QUẢN LÝ THÁNG CỦA HÓA ĐƠN", frm);
            splashScreenManager1.CloseWaitForm();
        }

        private void iStats_ItemClick(object sender, ItemClickEventArgs e)
        {
            splashScreenManager1.ShowWaitForm();
            frmStatistics frm = new frmStatistics();
            addForm("THỐNG KÊ DOANH THU", frm);
            splashScreenManager1.CloseWaitForm();
        }

        private void iLiabilities_ItemClick(object sender, ItemClickEventArgs e)
        {
            splashScreenManager1.ShowWaitForm();
            frmManageLiabilities frm = new frmManageLiabilities();
            addForm("QUẢN LÝ CÔNG NỢ", frm);
            splashScreenManager1.CloseWaitForm();
        }

        private void iLogin_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmLogin frm = new frmLogin();
            frm.ShowDialog();
        }

        private void iSettingConnect_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmConnectDatabase frm = new frmConnectDatabase();
            frm.ShowDialog();
        }

    }
}